/*
 * Copyright (c) 2011, Nikolaus Moll
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the gradecalculator.net nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.gradecalculator.plugin.core.factory.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.gradecalculator.core.interfaces.ICourse;
import net.gradecalculator.core.interfaces.IModuleList;
import net.gradecalculator.core.interfaces.IStudent;
import net.gradecalculator.core.interfaces.IStudyRegulations;
import net.gradecalculator.core.interfaces.IUniversity;
import net.gradecalculator.core.interfaces.results.IResult;
import net.gradecalculator.core.interfaces.results.IResultType;

public class CoreStudent implements IStudent {
	private String name = "";
	private IUniversity university = new CoreUniversity();
	private String degree = "";
	private String courseOfStudies = "";
	private String versionOfRegulations = ""; // version of regulations
	private List<IModuleList> selectedModuleLists = new LinkedList<IModuleList>();
	private List<IResult> results = new ArrayList<IResult>();
	private List<IResultType> resultTypes;
	private IStudyRegulations regulations;

	private final Map<ICourse, IResult> resultCache = new HashMap<ICourse, IResult>();

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(final String name) {
		this.name = name;
	}

	@Override
	public String getVersionOfRegulations() {
		return versionOfRegulations;
	}

	@Override
	public void setVersionOfRegulations(final String version) {
		this.versionOfRegulations = version;
	}

	@Override
	public List<IModuleList> getElectiveModulesAreas() {
		return selectedModuleLists;
	}

	@Override
	public void setElectiveModulesAreas(final List<IModuleList> selectedModuleLists) {
		this.selectedModuleLists = selectedModuleLists;
	}

	@Override
	public List<IResult> getResults() {
		return results;
	}

	@Override
	public void setResults(final List<IResult> results) {
		this.results = results;
	}

	@Override
	public IUniversity getUniversity() {
		return university;
	}

	@Override
	public void setUniversity(final IUniversity university) {
		this.university = university;
	}

	@Override
	public String getCourseOfStudies() {
		return courseOfStudies;
	}

	@Override
	public void setCourseOfStudies(final String courseOfStudies) {
		this.courseOfStudies = courseOfStudies;
	}

	@Override
	public String getDegree() {
		return degree;
	}

	@Override
	public void setDegree(final String degree) {
		this.degree = degree;
	}

	@Override
	public void addElectiveModulesArea(final IModuleList electiveModuleArea) {
		selectedModuleLists.add(electiveModuleArea);
	}

	@Override
	public void removeElectiveModulesArea(final IModuleList electiveModuleArea) {
		selectedModuleLists.remove(electiveModuleArea);
	}

	@Override
	public boolean hasElectiveModulesArea(final IModuleList electiveModuleArea) {
		return selectedModuleLists.contains(electiveModuleArea);
	}

	@Override
	public void addResult(final IResult result) {

		results.add(result);
	}

	@Override
	public void removeResult(final IResult result) {
		results.remove(result);
	}

	@Override
	public List<IResultType> getResultTypes() {
		return resultTypes;
	}

	@Override
	public void setResultTypes(final List<IResultType> types) {
		this.resultTypes = types;
	}

	@Override
	public IStudyRegulations getRegulations() {
		return regulations;
	}

	@Override
	public void setRegulations(final IStudyRegulations regulations) {
		this.regulations = regulations;
	}

	@Override
	public IResult getResult(final ICourse course) {
		// TOOD clear cache and remove items from cache ...
		if (resultCache.containsKey(course)) {
			return resultCache.get(course);
		}

		for (final IResult result : results) {
			if (result.getCourse() == course) {
				resultCache.put(course, result);
				return result;
			}
		}

		// TODO Auto-generated method stub
		//final IResult result = GCCore.getFactory().createResult();
		return null;
	}

}
